package org.erp.procurement.entity;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;
import org.erp.common.basic.BaseEntity;
import org.erp.common.enums.SysWhetherEnum;
import org.erp.procurement.entity.bo.MiniOrderAddBo;
import org.erp.procurement.entity.bo.OrderProductDetailsAddBo;

/**
 * 订单产品详情表
 *
 * @author libin
 * @version 1.0
 * @description: 订单产品详情表
 * @date 2025/11/5 16:59
 **/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
@TableName(value = "order_product_details", autoResultMap = true)
public class OrderProductDetails extends BaseEntity {
    /**
     * 自增id
     */
    @TableId(type = IdType.AUTO)
    private Long id;
    /**
     * 采购订单id
     */
    private String orderNo;
    /**
     * 产品名称
     */
    private String productName;
    /**
     * 品牌id
     */
    private Long brandId;
    /**
     * 品类id
     */
    private Long categoryId;
    /**
     * 机型id
     */
    private Long modelId;
    /**
     * 系列id
     */
    private Long seriesId;
    /**
     * 等级id
     */
    private Long levelId;
    /**
     * 产品IMEI
     */
    private String productImei;
    /**
     * 背贴码
     */
    private String productCode;
    /**
     * 产品颜色
     */
    private String productColor;
    /**
     * 产品内存
     */
    private String productStorage;
    /**
     * 回收金额
     */
    private Integer recycleAmount;
    /**
     * 租户id
     */
    private Long tenantId;
    /**
     * 租户名称
     */
    private String tenantName;
    /**
     * 第三方平台订单id
     */
    private String thirdOrderNo;
    /**
     * 是否代销(0否1是)
     */
    private Integer isSell;
    /**
     * 质检状态
     */
    private Integer checkStatus;
    /**
     * 验机人
     */
    private String checkPerson;
    /**
     * 是否入库
     */
    private Integer pushStock;

    public static OrderProductDetails addOneBuild(String orderNo, MiniOrderAddBo miniOrderAddBo) {
        JSONObject report = JSONObject.parseObject(miniOrderAddBo.getReport());
        return OrderProductDetails.builder()
                .orderNo(orderNo)
                .productName(StringUtils.isNotBlank(miniOrderAddBo.getProductName()) ? miniOrderAddBo.getProductName() : "")
                .brandId(miniOrderAddBo.getBrandId())
                .categoryId(miniOrderAddBo.getCategoryId())
                .modelId(miniOrderAddBo.getModelId())
                .seriesId(miniOrderAddBo.getSeriesId())
                .levelId(miniOrderAddBo.getLevelId())
                .productImei("")
                .productCode("")
                .productColor(StringUtils.isNotBlank(report.getString("productColor")) ? report.getString("productColor") : "")
                .productStorage(StringUtils.isNotBlank(report.getString("productStorage")) ? report.getString("productStorage") : "")
                .recycleAmount(0)
                .thirdOrderNo("")
                .isSell(SysWhetherEnum.NO.getCode())
                .checkStatus(SysWhetherEnum.NO.getCode())
                .checkPerson("")
                .pushStock(SysWhetherEnum.NO.getCode())
                .build();
    }

    public static OrderProductDetails addBatchBuild(String orderNo, OrderProductDetailsAddBo orderProductDetailsAddBo) {
        return OrderProductDetails.builder()
                .orderNo(orderNo)
                .productName(StringUtils.isNotBlank(orderProductDetailsAddBo.getProductName()) ? orderProductDetailsAddBo.getProductName() : "")
                .productImei(StringUtils.isNotBlank(orderProductDetailsAddBo.getProductImei()) ? orderProductDetailsAddBo.getProductImei() : "")
                .productCode(StringUtils.isNotBlank(orderProductDetailsAddBo.getProductCode()) ? orderProductDetailsAddBo.getProductCode() : "")
                .productColor(StringUtils.isNotBlank(orderProductDetailsAddBo.getProductColor()) ? orderProductDetailsAddBo.getProductColor() : "")
                .productStorage(StringUtils.isNotBlank(orderProductDetailsAddBo.getProductStorage()) ? orderProductDetailsAddBo.getProductStorage() : "")
                .recycleAmount(0)
                .thirdOrderNo("")
                .isSell(SysWhetherEnum.NO.getCode())
                .checkStatus(SysWhetherEnum.NO.getCode())
                .checkPerson("")
                .build();
    }

}
